<?php

namespace App\Http\Controllers;

use App\Assignment;
use App\AssignmentCompletion;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AssignmentCompletionController extends Controller
{
    public function update(Request $request, Assignment $assignment)
    {
        $completion = AssignmentCompletion::firstOrNew([
            'assignment_id' => $assignment->id,
            'student_id' => Auth::id()
        ]);

        $completion->is_completed = $request->input('is_completed', true);
        $completion->completed_at = $completion->is_completed ? now() : null;
        $completion->save();

        return redirect()->back()
            ->with('success', '作业状态更新成功');
    }
}
